package com.gj.demojuc.chap02;

/**
 * 演示死锁
 */
public class DeadLockDemo1 {
    //两把对象锁
    private static final Object lock1 = new Object();
    private static final Object lock2 = new Object();
    //两个互相调用的方法
    public static void main(String[] args) {
        new Thread(()->{
            synchronized (lock1){
                System.out.println("Thread 1 Holding lock1");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("Thread 1 Waiting lock2");
                synchronized (lock2){
                    System.out.println("Thread 1 Both locks acquired");
                }
            }
        }).start();

        new Thread(()->{
            synchronized (lock2){
                System.out.println("Thread 2 Holding lock2");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("Thread 2 Waiting lock1");
                synchronized (lock1){
                    System.out.println("Thread 2 Both locks acquired");
                }
            }
        }).start();
    }
}
